<?php
namespace app\service;

/*
 *   红方	黑方
 *   帅 K	将k
 *   仕A		士a
 *   相B		象b
 *   马N		马n
 *   车R		车r
 *   炮C		炮c
 *   兵P		卒p
 */

class ChessHelper
{
	public static $royhoo = 'luoyihu';
	/**
	 * 初始棋局数组
	 */
	public static $board = array
	(
		array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
		array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
		array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
		array(0,0,0,39,37,35,33,32,34,36,38,40,0,0,0,0),
		array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
		array(0,0,0,0,41,0,0,0,0,0,42,0,0,0,0,0),
		array(0,0,0,43,0,44,0,45,0,46,0,47,0,0,0,0),
		array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
		array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
		array(0,0,0,27,0,28,0,29,0,30,0,31,0,0,0,0),
		array(0,0,0,0,25,0,0,0,0,0,26,0,0,0,0,0),
		array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
		array(0,0,0,23,21,19,17,16,18,20,22,24,0,0,0,0),
		array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
		array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
		array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
	);
	
	/**
	 * 棋子的初始整数值，用于将FEN串转换为相应的数字
	 */
	public static $charToInt = array(
			'K'=>16,
			'A'=>17,
			'B'=>19,
			'N'=>21,
			'R'=>23,
			'C'=>25,
			'P'=>27,
			'k'=>32,
			'a'=>33,
			'b'=>35,
			'n'=>37,
			'r'=>39,
			'c'=>41,
			'p'=>43
	);
	
	/**
	 * 将二维数组转为一维数组
	 */
	public static function twodivToOne($twoDiv)
	{
		$oneDiv = array();
		foreach ($twoDiv as $value)
		{
			$oneDiv = array_merge($oneDiv, $value);
		}
		return $oneDiv;
	}
	
	/**
	 * 将一维数组表示的局面转换成FEN串
	 * @param int $side 轮到哪方走，0表示红方，1表示黑方
	 * @param array(256) $board 一维棋盘数组
	 * @return string $fenString 局面的FEN串格式
	 */
	public static function arrayToString($side, $board)
	{
		$fenString = "";
		$k = 0;		// 连续无棋子的数量
		$pc = 0;	// 数字表示的棋子
		for($i=3; $i<=12; $i++)
		{
			$k = 0;
			for($j=3; $j<=11; $j++)
			{
				$pc = $board[($i<<4)+$j];
				if($pc != 0)
				{
					if($k > 0)
					{
						$fenString .= $k;
						$k = 0;
					}
					$fenString .= self::intToChar($pc);
				}
				else
				{
					$k++;
				}
			}
			if($k > 0)
			{
				$fenString .= $k;
			}
			$fenString .= '/';
		}
		$fenString = rtrim($fenString, '/');
		$fenString .= ' ';
		$fenString .= ($side==0?'w':'b');
		return $fenString;
	}
	
	/**
	 * 求得整数对应的字符
	 * @param int $value
	 * @return string
	 */
	public static function intToChar($value)
	{
		if($value<32)
		{
			switch($value)
			{
			case 16:
				return 'K';
			case 17:
			case 18:
				return 'A';
			case 19:
			case 20:
				return 'B';
			case 21:
			case 22:
				return 'N';
			case 23:
			case 24:
				return 'R';
			case 25:
			case 26:
				return 'C';
			case 27:
			case 28:
			case 29:
			case 30:
			case 31:
				return 'P';
			default:
				return 0;
			} 
		}
		else
		{
			$value = $value - 16;
			switch($value)
			{
				case 16:
					return 'k';
				case 17:
				case 18:
					return 'a';
				case 19:
				case 20:
					return 'b';
				case 21:
				case 22:
					return 'n';
				case 23:
				case 24:
					return 'r';
				case 25:
				case 26:
					return 'c';
				case 27:
				case 28:
				case 29:
				case 30:
				case 31:
					return 'p';
				default:
					return 0;
			}
		}
	}

	/**
	 * 将FEN串表示的局面转换成一维数组
	 * @param string $fenString
	 * @param &int $side 走棋方
	 * @param &array $fenArray 棋盘数组
	 * @param &array $pieceArray
	 */
	public static function stringToArray($fenString, &$side, &$fenArray, &$pieceArray)
	{
		$charToIntMap = self::$charToInt;
		if($fenString == '')
		{
			return;
		}
		$fenStringArray = str_split($fenString, 1);
		$i = 3;	// 行
		$j = 3;	// 列
		//$fenArray = array_fill(0, 256, 0);
		foreach ($fenStringArray as $key=>$fenChar)
		{
			if($fenChar == ' ')
			{
				break;
			}
			if($fenChar == '/')
			{
				$j = 3;
				$i++;
				if($i > 12)
				{
					break;
				}
			}
			else if ($fenChar >= '1' && $fenChar <= '9' )
			{
				$j += $fenChar;
			}
			else if (($fenChar >= 'A' && $fenChar <= 'Z') || ($fenChar >= 'a' && $fenChar <= 'z'))
			{
				if($j <= 11)
				{
					$fenArray[($i<<4) + $j] = $charToIntMap[$fenChar];
					$pieceArray[$charToIntMap[$fenChar]] = ($i<<4) + $j;
					$charToIntMap[$fenChar] = $charToIntMap[$fenChar] + 1;
					$j++;
				}
			}
		}
		$key++;
		if($fenStringArray[$key] == 'b')
		{
			$side = 1;
		} 
		else
		{
			$side = 0;
		}
		//return $fenArray;
	}

}